package com.pub.functions;

public class NumFuncs {

	public static byte calSum(byte[] bytes) {
		return NumFuncs.calSum(bytes, bytes.length);
	}

	public static byte calSum(byte[] bytes, int len) {
		byte sum = 0;

		for (int i = 0; i < len; i++) {
			sum ^= bytes[i];
		}

		return sum;
	}

	public static byte GetSum(byte[] pdata, byte len) {
		byte sum = 0;
		int i;

		sum = 0;
		for (i = 0; i < len; i++) {
			sum += pdata[i];
		}
		return sum;
	}

	public static byte[] convertBytesOrder(byte[] data) {
		return convertBytesOrder(data, data == null ? 0 : data.length);
	}

	public static byte[] convertBytesOrder(byte[] data, int len) {
		if (data == null || len > data.length) {
			return new byte[0];
		}

		byte[] retData = new byte[len];
		for (int i = 0; i < len; i++) {
			retData[i] = data[len - i - 1];
		}

		return retData;
	}

	public static boolean isNum(String str) {
		return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
	}
}
